
use strict;



my %unsupported_ua = ();

if($#ARGV < 0) {
  #print "Usage: $0 goodua.txt number.txt ...";
  #exit ;
}

open UA, "$ARGV[0]" || die "Open file $ARGV[0] error!: $!";
my @good_ua = <UA>;
close UA || die "Close file $ARGV[0] error!: $!";

@good_ua = grep(s/\s+$//g, @good_ua);

processDir('./');

my $bad_ua_file = "bad_ua.txt";
open (BAD_UA, ">$bad_ua_file")         || die "can't open $bad_ua_file: $!";
foreach my $key (sort(keys %unsupported_ua)) {
  print BAD_UA $key, "\r\n";
}
close(BAD_UA)                || die "can't close $bad_ua_file: $!";


sub processDir {
  my $dir = shift;
  
  opendir(DIRHANDLE , $dir) || die "Open dir $dir error!: $!";
  my @files = grep {!/^\./} readdir(DIRHANDLE);
  closedir(DIRHANDLE) || die "Close dir $dir error!: $!";
  
  
  for my $file (@files) {
    my $fullFileName = $dir . $file;
    if( -d $fullFileName ) {
      processDir($fullFileName);
    } else {
      processNumber($dir . '/', $file);
    } 
  }  
  
}


sub processNumber{
  my $dir = shift;
  my $file = shift;
  my $fileGood = 'good_' . $file;
  my $fileBad = 'bad_' . $file;
  #$dir =~ s/^\.\///; 
  open (INPUT, $dir . $file)             || die "can't open $dir$file: $!";
  open (OUTPUT, ">$dir$fileGood")     || die "can't open $dir$fileGood: $!";
  open (BAD_OUTPUT, ">$dir$fileBad")  || die "can't open $dir$fileBad: $!";
  while (my $line = <INPUT>) {
    my $isGoodUa = 0;
    foreach (@good_ua) {
      if(-1 != index($line,$_)) {
        $isGoodUa = 1;
        last;
      }
    }
    if($isGoodUa == 1) {
      print OUTPUT $line;
    } else {
      print BAD_OUTPUT $line;
      my @params= split /,/, $line;
      $params[2] =~ s/\"//g ;      
      $unsupported_ua{$params[2]} = 0; 
    }
  }
  close(BAD_OUTPUT) || die "can't close $dir$fileBad: $!";
  close(OUTPUT) || die "can't close $dir$fileGood: $!";
  close(INPUT) || die "can't close $dir$file: $!";
}




